package com.theeviljames.coursework.ann;


import java.util.Random;

public class ANNLayer{
	
	private double[][] outputs;
	private boolean bias;
	private boolean inputLayer;
	private double[][] weights;
	private double[][] weightChange;
	private static final Random rand  = new Random();
	private double[][] f;
	private double[][] deltaError;
	
	public ANNLayer(int inputs, int outputs, boolean bias, boolean inputLayer) {
		this.bias = bias;
		this.inputLayer = inputLayer;
		if(bias){
			weights = ANNMatrix.random(inputs+1, outputs, 1, rand);
			weightChange = new double[inputs+1][outputs];
		}
		else{
			weights = ANNMatrix.random(inputs, outputs, 1, rand);	
			weightChange = new double[inputs][outputs];
		}
	}
	
	public double[][] getOutputs(double[][] inputs){
		if(!inputLayer)inputs = ANNMatrix.sigmoid(inputs);
		if(bias)inputs = addBias(inputs);
		f = inputs; //Save f for backpropagating the error
		//O = W'I  ' stands for transpose
		//In other words Output = Weights' x Inputs
		try{outputs = ANNMatrix.times(ANNMatrix.transpose(weights), inputs);}
		catch(Exception e){e.printStackTrace();}
		return outputs;
	}
	
	public double[][] f(){
		return f;
	}
	
	public double[][] addBias(double[][] inputs){
		double[][] newInputs = new double[inputs.length+1][1];
		for(int i = 0; i < inputs.length; i++)newInputs[i][0] = inputs[i][0];
		newInputs[inputs.length][0] = 1;
		return newInputs;
	}
	
	public void setWeightChange(double[][] wc){
		weightChange = wc;
	}

	public void setWeights(double[][] w){
		weights = w;
	}

	public double[][] getDeltaError() {
		return deltaError;
	}

	public void setDeltaError(double[][] deltaError) {
		this.deltaError = deltaError;
	}

	public double[][] getWeightChange() {
		return weightChange;
	}

	public double[][] getWeights() {
		return weights;
	}
}
